package cn.gzmli.dtt.common.service;

import cn.gzmli.dtt.common.utils.BaseService;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Permission;
import cn.gzmli.dtt.common.utils.BaseUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
        *
        * Title:PermissionService
        * Description: 权限service
        * @author:superKing_zhanglh
        * @date 2018年4月3日
        */
@Service
@Transactional
public class PermissionService extends BaseService<Permission> {

    public List<Map<String, Object>> getByCondition(HttpServletRequest request, PageInfo pageInfo) throws Exception {
        Map<String, Object> condition = this.getCondition(request);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM(")
                .append("SELECT p.*,")
                .append("m.name AS menuName ")
                .append("FROM permission p ")
                .append("LEFT JOIN menu m ON m.id = p.menu_id  ")
                .append("GROUP BY p.id)tab ")
                .append("WHERE 1=1 AND  permission_code !='' and permission_code is not null");
        if(pageInfo == null){
            return BaseUtil.getData(sql, condition, this.getDao(), "id", false);
        }
        return BaseUtil.getData(sql, condition, pageInfo, this.getDao(), "id", false);
    }



    public List<Map<String, Object>> getTree(HttpServletRequest request) throws Exception {
        String sql = "SELECT p.*,p.permission_name as name,m.name as menuName FROM permission p LEFT JOIN menu m ON p.menu_id = m.id";
        return this.getDao().getJdbcTemplate().queryForList(sql);
     /*   Map<String, Object> condition = this.getCondition(request);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT CONCAT(id, 'dttD') AS id,")
            .append("id AS realID,")
            .append("permission_name AS name,")
            .append("CONCAT(pId, 'dttD') AS pId,")
            .append("'Permission' AS nodeType ")
            .append("FROM Permission ")
            .append("WHERE 1=1 ");
        return BaseUtil.getData(sql, condition, this.getDao());*/
    }


    private Map<String, Object> getCondition(HttpServletRequest request) throws Exception {
        Map<String, Object> condition = new HashMap<>();
        if (StrUtil.isNotBlank(request.getParameter("permission_name"))){
            condition.put(" AND permission_name LIKE ?", "%"+request.getParameter("permission_name")+"%");
        }else if (StrUtil.isNotBlank(request.getParameter("permission_code"))){
            condition.put(" AND permission_code LIKE ?", "%"+request.getParameter("permission_code")+"%");
        }
        if (StrUtil.isNotBlank(request.getParameter("menu_id"))){
            condition.put(" AND menu_id = ?", request.getParameter("menu_id"));
        }
        return condition;
    }

}